package com.woniu.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.woniu.entity.PageResult;
import com.woniu.entity.Result;
import com.woniu.pojo.Space;
import com.woniu.service.IMagnetService;
import com.woniu.service.ISpaceService;
import com.woniu.vo.SpaceQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lqg
 * @since 2022-10-12
 */
@RestController
@RequestMapping("/space")
@Api(tags = "泊位管理")
public class SpaceController extends BaseController{

    @Autowired
    private ISpaceService spaceService;
    @Autowired
    private IMagnetService magnetService;

    @PostMapping("/search/{currentPage}/{pageSize}")
    @ApiOperation("分页条件查询")
    public Result search(
            @PathVariable Integer currentPage,
            @PathVariable Integer pageSize,
            @RequestBody SpaceQueryVo spaceQueryVo) {
        IPage<Space> page = spaceService.search(currentPage, pageSize, spaceQueryVo);
        PageResult pageResult = new PageResult();
        if (page != null) {
            pageResult.setTotal(page.getTotal());
            pageResult.setData(page.getRecords());
        } else {
            pageResult.setTotal(0L);
            pageResult.setData(null);
        }

        return toDataResult(pageResult);
    }

    @PostMapping
    @ApiOperation("增加泊位")
    public Result add(@RequestBody Space space) {
        return toResult(spaceService.save(space));
    }

    @DeleteMapping("/{id}")
    @ApiOperation("根据id删除")
    public Result remove(@PathVariable Long id) {
        return toResult(spaceService.removeById(id));
    }

    @PutMapping
    @ApiOperation("根据id修改")
    public Result edit(@RequestBody Space space) {
        return toResult(spaceService.updateById(space));
    }

    @GetMapping("/{id}")
    @ApiOperation("根据id查询")
    public Result getById(@PathVariable Long id) {
        return toDataResult(spaceService.getById(id));
    }

    @GetMapping("/magnet")
    @ApiOperation("获取地磁")
    public Result getMagnet() {
        return toDataResult(magnetService.list());
    }
}
